package com.ljf.authority.infra.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.authority.domain.repository.AuthorityBaseMenuTabulationRepository;
import com.ljf.authority.dto.query.AuthorityBaseMenuTabulationQuery;
import com.ljf.authority.infra.mapper.AuthorityBaseMenuTabulationMapper;
import com.ljf.authority.model.AuthorityBaseMenuTabulation;
import com.ljf.authority.vo.AuthorityBaseMenuTabulationVO;
import com.ljf.common.enums.ActivationEnum;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.infra.repository.impl.BasisRepositoryImpl;
import com.ljf.common.utils.CollectionUtil;
import com.ljf.common.utils.EnumUtil;
import com.ljf.common.utils.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 菜单列表定义  数据访问接口实现
 *
 * @author LJF
 * @since 2024-10-17
 */
@Component
public class AuthorityBaseMenuTabulationRepositoryImpl extends BasisRepositoryImpl<AuthorityBaseMenuTabulationMapper, AuthorityBaseMenuTabulation, AuthorityBaseMenuTabulationQuery, AuthorityBaseMenuTabulationVO> implements AuthorityBaseMenuTabulationRepository {

    @Override
    protected QueryWrapper<AuthorityBaseMenuTabulation> fillWrapper(AuthorityBaseMenuTabulationQuery query, boolean sort) throws ServiceException {
        QueryWrapper<AuthorityBaseMenuTabulation> wrapper = super.fillWrapper(query, sort);
        if (CollectionUtil.isNotEmpty(query.getLevelList())) {
            wrapper.in(AuthorityBaseMenuTabulation.COL_LEVEL, query.getLevelList());
        }
        if (CollectionUtil.isNotEmpty(query.getCodeList())) {
            wrapper.in(AuthorityBaseMenuTabulation.COL_CODE, query.getCodeList());
        }
        if (StringUtil.isNotEmpty(query.getNameLike())) {
            wrapper.like(AuthorityBaseMenuTabulation.COL_NAME, query.getNameLike());
        }
        if (CollectionUtil.isNotEmpty(query.getPCodeList())) {
            wrapper.in(AuthorityBaseMenuTabulation.COL_P_CODE, query.getPCodeList());
        }
        if (StringUtil.isNotEmpty(query.getPCodeRightLike())) {
            wrapper.likeRight(AuthorityBaseMenuTabulation.COL_P_CODE, query.getPCodeRightLike());
        }
        if (CollectionUtil.isNotEmpty(query.getIds())) {
            wrapper.in(AuthorityBaseMenuTabulation.COL_ID, query.getIds());
        }
        return wrapper;
    }

    @Override
    protected void fillInfo(List<AuthorityBaseMenuTabulationVO> resultList) throws ServiceException {
        super.fillInfo(resultList);
        if (CollectionUtil.isEmpty(resultList)) {
            return;
        }
        Map<Integer, String> activationMap = EnumUtil.getCodeToNameMap(ActivationEnum.values());
        for (AuthorityBaseMenuTabulationVO result : resultList) {
            result.setActivationName(activationMap.getOrDefault(result.getActivation(), ""));
        }
    }

}
